$(document).ready(function(){
	//check for canvas support
    var canvasEl = document.createElement('canvas'); //create the canvas object
    if(!(canvasEl.getContext && canvasEl.getContext('2d'))){ //if the method is not supported, i.e canvas is not supported
        var msg = "OOPS!! Your browser does not support a feature that is used by our application. Why not try it in a newer browser - IE9+ onwards or the latest versions of Chrome, Safari, Firefox.";
        //alert('hi');
        $("body").html("<p class='errorMsg'>" + msg + "</p>");
        return;
    }
    //check for cookies
    if(!(navigator.cookieEnabled)){ // && navigator['cookieEnabled'] != null, this is really not needed
        var msg = "It seems you have disabled cookied in your browser. Cookies must be enabled in your browser for this app to function";
        $("body").html("<p class='errorMsg'>" + msg + "</p>");
        return;
    }

	//global vars
	var form = $("#registerForm");
	var fname = $("#firstname");
	var fnameInfo = $("#fnameInfo");
	var lname = $("#lastname");
    var lnameInfo = $("#lnameInfo");
    var city = $("#city");
    var cityInfo = $("#cityInfo");
	var email = $("#email");
	var emailInfo = $("#emailInfo");
	var age = $("#age");
    var ageInfo = $("#ageInfo");
    var agree = $("#agree");
    var agreeInfo = $("#agreeInfo");
	/*var pass1 = $("#pass1");
	var pass1Info = $("#pass1Info");
	var pass2 = $("#pass2");
	var pass2Info = $("#pass2Info");
	var message = $("#message");*/
	
	//On blur
	fname.blur(validateFirstName);
	lname.blur(validateLastName);
	city.blur(validateCity);
	email.blur(validateEmail);
	age.blur(validateAge);
	/*pass1.blur(validatePass1);
	pass2.blur(validatePass2);*/
	//On key press
	fname.keyup(validateFirstName);
	lname.keyup(validateLastName);
	city.keyup(validateCity);
	email.keyup(validateEmail);
	age.keyup(validateAge);
	/*pass1.keyup(validatePass1);
	pass2.keyup(validatePass2);
	message.keyup(validateMessage);  */
	//On Submitting
	form.submit(function(){
	    //alert('test');
		if(validateFirstName() & validateLastName() & validateCity() & validateEmail() & validateAge() & validateAgreement()) { //& validateEmail() & validatePass1() & validatePass2() & validateMessage())
			return true;
        }
		else {
			return false;
        }
	});
	
	//validation functions
	function validateEmail(){
		//testing regular expression
		var a = $("#email").val();
		var filter = /^[a-zA-Z0-9]+[a-zA-Z0-9_.-]+[a-zA-Z0-9_-]+@[a-zA-Z0-9]+[a-zA-Z0-9.-]+[a-zA-Z0-9]+.[a-z]{2,4}$/;
		//if it's valid email
		if(filter.test(a)){
			email.removeClass("error");
			emailInfo.text("We will not spam you");
			//emailInfo.removeClass("error");
			return true;
		}
		//if it's NOT valid
		else{
			email.addClass("error");
			emailInfo.text("Type a valid e-mail");
			emailInfo.addClass("error");
			return false;
		}
	}
	function validateFirstName(){
		//if it's NOT valid
		if(fname.val().length < 3){
			fname.addClass("error");
			fnameInfo.text("* (3 chars minimum)");
			fnameInfo.addClass("error");
			return false;
		}
		//if it's valid
		else{
			fname.removeClass("error");
			fnameInfo.text("");
			fnameInfo.removeClass("error");
			return true;
		}
	}
	function validateLastName(){
        //if it's NOT valid
        if(lname.val().length < 3){
            lname.addClass("error");
            lnameInfo.text("* (3 chars minimum)");
            lnameInfo.addClass("error");
            return false;
        }
        //if it's valid
        else{
            lname.removeClass("error");
            lnameInfo.text("");
            lnameInfo.removeClass("error");
            return true;
        }
    }

    function validateCity(){
        //if it's NOT valid
        if(city.val().length < 1){
            city.addClass("error");
            cityInfo.text("*");
            cityInfo.addClass("error");
            return false;
        }
        //if it's valid
        else{
            city.removeClass("error");
            cityInfo.text("");
            cityInfo.removeClass("error");
            return true;
        }
    }

    function validateAge(){
        //if it's NOT valid
        var filter = /^\d{0,3}$/;
        //console.log(filter.test(age.val()));
        if(!filter.test(age.val())){
            age.addClass("error");
            ageInfo.text("Enter valid age");
            ageInfo.addClass("error");
            return false;
        }
        //if it's valid
        else{
            age.removeClass("error");
            ageInfo.text("Optional");
            //ageInfo.removeClass("error");
            return true;
        }
    }

    function validateAgreement() {
        if(!agree.is(':checked')){
            agree.addClass('error');
            agreeInfo.text("* (Must Agree)");
            agreeInfo.addClass("error");
            return false;
        }
        //if it's valid
        else{
            agreeInfo.removeClass("error");
            agreeInfo.text("");
            agreeInfo.removeClass("error");
            return true;
        }
    }

});